package com.view.logmanage;

import com.bean.Record;
import com.bean.Stock;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.service.RecordService;
import com.service.impl.RecordServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class LogManagePanel extends JPanel {
    private MainFrame frame;

    private LovoTable logTable;


    private JPanel queryPanel;
    private JLabel beginLab;
    private JTextField beginTxt;
    private JLabel endLab;
    private JTextField endTxt;
    private JButton queryBtn;

    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Record> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private RecordService recordService = new RecordServiceImpl();

    public LogManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        setPageUtilSuper();
        this.initTable();
        this.initBtn();
        this.initQueryPanel();

        txtPage = new JLabel();
        this.txtPage.setBounds(130, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        java.util.List<Record> stocks = new ArrayList<>();
        stocks = recordService.getAllRecord();
        pageUtilSuper = new PageUtilSuper(pageSize,stocks);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        java.util.List<Record> stocks = new ArrayList<>();
        stocks = pageUtilSuper.getPgNumberThing(pgNumber);
        logTable.updateLovoTable(stocks);
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.logTable = new LovoTable(this, new String[]{"日期", "内容"
                }, new String[]{"date", "info"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<Record> list = new ArrayList<>();
        list = pageUtilSuper.getPgNumberThing(1);
        logTable.updateLovoTable(list);
        this.logTable.setSizeAndLocation(0, 0, 800, 300);
    }

    private void initBtn() {
        // TODO Auto-generated method stub
        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);

        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });

    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询日志信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(500, 310, 250, 180);
        this.add(this.queryPanel);

        this.beginLab = new JLabel("起始日期");
        this.beginLab.setBounds(20, 20, 60, 20);
        this.queryPanel.add(this.beginLab);
        this.beginTxt = new JTextField();
        this.beginTxt.setBounds(100, 20, 120, 20);
        this.queryPanel.add(this.beginTxt);

        this.endLab = new JLabel("结束日期");
        this.endLab.setBounds(20, 70, 60, 20);
        this.queryPanel.add(this.endLab);
        this.endTxt = new JTextField();
        this.endTxt.setBounds(100, 70, 120, 20);
        this.queryPanel.add(this.endTxt);




        this.queryBtn = new JButton("查找");
        this.queryBtn.setBounds(10, 130, 80, 20);
        this.queryPanel.add(this.queryBtn);

        queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String beginTxtText = beginTxt.getText();
                String endTxtText = endTxt.getText();
                List<Record> list = new ArrayList<>();
                list = recordService.findRecords(beginTxtText,endTxtText);
                logTable.updateLovoTable(list);
            }
        });
    }
}
